package com.ourteam.product.requirement.dao.impl;

import com.ourteam.product.requirement.dao.IProductDefineDAO;
import com.ourteam.product.requirement.dao.ProductDefine;
import com.ourteam.product.requirement.dao.ProductDefineDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-05-03 16:45:36
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductDefineDAOJdbcImpl extends AbstractSpringDAO
    implements IProductDefineDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, CatalogId, ProductCode, ProductName, Remarks, Status,
            SubmitterId, CreatorName, CreateTime, LastModifierName,
            LastModifyTime
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.CatalogId,
            _DataObjectProperty.ProductCode, _DataObjectProperty.ProductName,
            _DataObjectProperty.Remarks, _DataObjectProperty.Status,
            _DataObjectProperty.SubmitterId, _DataObjectProperty.CreatorName,
            _DataObjectProperty.CreateTime, _DataObjectProperty.LastModifierName,
            _DataObjectProperty.LastModifyTime
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductDefine, ProductDefineDAOQueryBean> daoHelper;
    private com.ourteam.product.requirement.dao.impl.AbstractProductVersionDAOJdbcImpl ProductVersionDao;

    public AbstractProductDefineDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductDefine, ProductDefineDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductDefine
         * @throws Exception
         */
    public boolean checkIsUnique(ProductDefine aProductDefine)
        throws Exception {
        if (aProductDefine == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductDefine.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductDefine
     * @throws Exception
     */
    public void insert(ProductDefine aProductDefine) throws Exception {
        super.doInsert(aProductDefine);
    }

    /**
         * Batch Insert Datas
         * @param aProductDefine
         * @throws Exception
         */
    public void batchInsert(ProductDefine[] aProductDefines)
        throws Exception {
        super.doBatchInsert(aProductDefines);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductDefineDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductDefine aProductDefine) throws Exception {
        delete(aProductDefine.getId());
    }

    /**
     * Update Data Selective
     * @param aProductDefine
     * @throws Exception
     */
    public void update(ProductDefine aProductDefine) throws Exception {
        if (aProductDefine.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductDefine);

        ProductDefine tempObj = findById(aProductDefine.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductDefine, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductDefines[]
     * @throws Exception
     */
    public void batchUpdate(ProductDefine[] aProductDefines)
        throws Exception {
        super.doBatchUpdate(aProductDefines);
    }

    /**
         * Update Data By Condictions
         * @param aProductDefine
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductDefine aProductDefine,
        ProductDefineDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aProductDefine, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductDefineDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductDefine aProductDefine)
        throws Exception {
        if (aProductDefine.getProductCode() == null) {
            throw new Exception(
                "The ProductDefine.productCode value is not allow NULL");
        }

        if (aProductDefine.getProductName() == null) {
            throw new Exception(
                "The ProductDefine.productName value is not allow NULL");
        }

        if (aProductDefine.getRemarks() == null) {
            throw new Exception(
                "The ProductDefine.remarks value is not allow NULL");
        }

        if (aProductDefine.getStatus() == null) {
            throw new Exception(
                "The ProductDefine.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductDefine findById(final long id) throws Exception {
        ProductDefineDAOQueryBean queryBean = new ProductDefineDAOQueryBean();

        ProductDefineDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductDefine[] ProductDefines = query(queryBean);

        if (ProductDefines.length == 0) {
            return null;
        } else {
            return ProductDefines[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductDefine[]
         * @throws Exception
         */
    public ProductDefine[] query(ProductDefineDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcProductDefineRowMapper());

        return (ProductDefine[]) result.toArray(new ProductDefine[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductDefineDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductDefineDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductDefine[] aProductDefines,
        ProductDefineDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductDefines, queryBean);
    }

    public void synchronize(ProductDefine[] aProductDefines,
        ProductDefineDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductDefine> callBack)
        throws Exception {
        daoHelper.synchronize(aProductDefines, queryBean, callBack);
    }

    public void setProductVersionDao(
        com.ourteam.product.requirement.dao.IProductVersionDAO ProductVersionDao) {
        this.ProductVersionDao = (com.ourteam.product.requirement.dao.impl.AbstractProductVersionDAOJdbcImpl) ProductVersionDao;
    }

    /**
     * Query Related ProductVersion
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.product.requirement.dao.ProductVersion[] queryProductVersion(
        ProductDefineDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.requirement.dao.IProductVersionDAO.ProductDefineId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.requirement.dao.impl.ProductVersionDAO.EntityName +
            " ref_ProductVersion";

        String[] entityFields = com.ourteam.product.requirement.dao.impl.ProductVersionDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.product.requirement.dao.impl.ProductVersionDAO.ENTITY_PROPERTIES;

        List list = ProductVersionDao.doQuery(entityName, entityFields,
                entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.product.requirement.dao.impl.ProductVersionDAO.JdbcProductVersionRowMapper());

        return (com.ourteam.product.requirement.dao.ProductVersion[]) list.toArray(new com.ourteam.product.requirement.dao.ProductVersion[list.size()]);
    }

    /**
     * Query Related ProductVersion Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] queryProductVersionSelective(
        ProductDefineDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.ProductDefineId,
                "productDefineId");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.ProductVersion,
                "productVersion");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.Remarks,
                "remarks");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.Status,
                "status");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.ProductVersionType,
                "productVersionType");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.ParentVersionId,
                "parentVersionId");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.StartDate,
                "startDate");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.EndDate,
                "endDate");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.ActualStartDate,
                "actualStartDate");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.ActualEndDate,
                "actualEndDate");

            queryBean.addSelectProperty(com.ourteam.product.requirement.dao.IProductVersionDAO.SortIndex,
                "sortIndex");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.requirement.dao.IProductVersionDAO.ProductDefineId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.requirement.dao.impl.ProductVersionDAO.EntityName +
            " ref_ProductVersion";

        return ProductVersionDao.doQuerySelective(entityName, queryBean,
            joinSQL.toString());
    }

    /**
     * Update Related ProductVersion
     * @param aProductVersion
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateProductVersion(
        com.ourteam.product.requirement.dao.ProductVersion aProductVersion,
        ProductDefineDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.requirement.dao.IProductVersionDAO.ProductDefineId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.requirement.dao.impl.ProductVersionDAO.EntityName +
            " ref_ProductVersion";

        String[] entityFields = com.ourteam.product.requirement.dao.impl.ProductVersionDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.product.requirement.dao.impl.ProductVersionDAO.ENTITY_PROPERTIES;

        ProductVersionDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aProductVersion, aQueryBean);
    }

    /**
     * Delete Related ProductVersion
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteProductVersion(ProductDefineDAOQueryBean aQueryBean)
        throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.requirement.dao.IProductVersionDAO.ProductDefineId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.requirement.dao.impl.ProductVersionDAO.EntityName; // + " ref_ProductVersion";

        ProductVersionDao.doDeleteByQuery(entityName, joinSQL.toString(),
            aQueryBean);
    }

    public static class JdbcProductDefineRowMapper implements RowMapper<ProductDefine> {
        public ProductDefine mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductDefine object = new ProductDefine();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object catalogId = null;

                catalogId = resultSet.getObject(columnIndex++);

                object.setCatalogIdNull(resultSet.getBoolean(columnIndex++));

                if (catalogId != null) {
                    setBeanValue(object, _DataObjectProperty.CatalogId,
                        catalogId);
                }

                Object productCode = null;

                productCode = resultSet.getObject(columnIndex++);

                object.setProductCodeNull(resultSet.getBoolean(columnIndex++));

                if (productCode != null) {
                    setBeanValue(object, _DataObjectProperty.ProductCode,
                        productCode);
                }

                Object productName = null;

                productName = resultSet.getObject(columnIndex++);

                object.setProductNameNull(resultSet.getBoolean(columnIndex++));

                if (productName != null) {
                    setBeanValue(object, _DataObjectProperty.ProductName,
                        productName);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object submitterId = null;

                submitterId = resultSet.getObject(columnIndex++);

                object.setSubmitterIdNull(resultSet.getBoolean(columnIndex++));

                if (submitterId != null) {
                    setBeanValue(object, _DataObjectProperty.SubmitterId,
                        submitterId);
                }

                Object creatorName = null;

                creatorName = resultSet.getObject(columnIndex++);

                object.setCreatorNameNull(resultSet.getBoolean(columnIndex++));

                if (creatorName != null) {
                    setBeanValue(object, _DataObjectProperty.CreatorName,
                        creatorName);
                }

                Object createTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    createTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    createTime = resultSet.getObject(columnIndex++);
                }

                object.setCreateTimeNull(resultSet.getBoolean(columnIndex++));

                if (createTime != null) {
                    setBeanValue(object, _DataObjectProperty.CreateTime,
                        createTime);
                }

                Object lastModifierName = null;

                lastModifierName = resultSet.getObject(columnIndex++);

                object.setLastModifierNameNull(resultSet.getBoolean(
                        columnIndex++));

                if (lastModifierName != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifierName,
                        lastModifierName);
                }

                Object lastModifyTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    lastModifyTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    lastModifyTime = resultSet.getObject(columnIndex++);
                }

                object.setLastModifyTimeNull(resultSet.getBoolean(columnIndex++));

                if (lastModifyTime != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifyTime,
                        lastModifyTime);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
